<?php
class ModelCatalogColory extends Model {
	public function getColory($colory_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "colory c LEFT JOIN " . DB_PREFIX . "colory_description cd ON (c.colory_id = cd.colory_id) LEFT JOIN " . DB_PREFIX . "colory_to_store c2s ON (c.colory_id = c2s.colory_id) WHERE c.colory_id = '" . (int)$colory_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'");
		
		return $query->row;
	}
	
	public function getColories($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "colory c LEFT JOIN " . DB_PREFIX . "colory_description cd ON (c.colory_id = cd.colory_id) LEFT JOIN " . DB_PREFIX . "colory_to_store c2s ON (c.colory_id = c2s.colory_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
		
		return $query->rows;
	}

	public function getColoriesByParentId($colory_id) {
		$colory_data = array();
		
		$colory_query = $this->db->query("SELECT colory_id FROM " . DB_PREFIX . "colory WHERE parent_id = '" . (int)$colory_id . "'");
		
		foreach ($colory_query->rows as $colory) {
			$colory_data[] = $colory['colory_id'];
			
			$children = $this->getColoriesByParentId($colory['colory_id']);
			
			if ($children) {
				$colory_data = array_merge($children, $colory_data);
			}			
		}
		
		return $colory_data;
	}
		
	public function getColoryLayoutId($colory_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "colory_to_layout WHERE colory_id = '" . (int)$colory_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");
		
		if ($query->num_rows) {
			return $query->row['layout_id'];
		} else {
			return $this->config->get('config_layout_colory');
		}
	}
					
	public function getTotalColoriesByColoryId($parent_id = 0) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "colory c LEFT JOIN " . DB_PREFIX . "colory_to_store c2s ON (c.colory_id = c2s.colory_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1'");
		
		return $query->row['total'];
	}
        
       
}
?>